# -*- coding: utf-8 -*-
# 姓名:谚语
# 开发时间:2022/11/13 9:46

from flask_restful import Resource
from flask_restful.reqparse import RequestParser

from comment.modules import db
from comment.modules.matched_result import Matched_result
from financial.resources.transaction.seralizer import MatchedResultPaginateSerializer


class MatchedResultResource(Resource):
    """匹配结果列表的搜索查询功能"""

    def get(self):
        rp = RequestParser()
        rp.add_argument('start')  # 起始时间
        rp.add_argument('end')  # 结束时间
        rp.add_argument('curPage')  # 当前页
        rp.add_argument('perPage')  # 每页显示数量

        args = rp.parse_args()
        start = args.get('start')
        end = args.get('end')
        cur_page = int(args.get('curPage'))
        per_page = int(args.get('perPage'))

        query = Matched_result.query
        # 根据起始时间和结束时间(得到时间区间)
        if start and end:
            # .cast(转换的数据, 转换的类型) 做类型转换     matchDate 匹配上的日期
            query = query.filter(db.cast(Matched_result.matchDate, db.DATE) >= db.cast(start, db.DATE)) \
                .filter(db.cast(Matched_result.matchDate, db.DATE) <= db.cast(end, db.DATE))

        # 分页处理      paginate 用于做分页处理
        result_list = query.paginate(cur_page, per_page, error_out=False)

        # 序列化
        data = MatchedResultPaginateSerializer(result_list).to_dict()

        return {'msg': 'success', 'data': data}
